package com.gemframework.modules.extent;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gemframework.model.common.BaseResultData;
import com.gemframework.model.entity.po.Grade;
import com.gemframework.model.entity.po.Users;
import com.gemframework.model.entity.vo.UsersVo;
import com.gemframework.service.GradeService;
import com.gemframework.service.SubjectService;
import com.gemframework.service.UsersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@CrossOrigin
@RequestMapping("/User")
public class UsersAPI {

    @Resource
    private UsersService usersService;
    @Resource
    private GradeService gradeService;
    @Resource
    private SubjectService subjectService;


    //发布表信息表条件分页
    @PostMapping("/usersPage")
    public IPage<Users> usersPage(@RequestBody UsersVo usersVo){
      return usersService.selUsersPage(usersVo);
    }


    //发布表信息表id集合查询
    @PostMapping("/listById")
    public BaseResultData listById(Integer id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return  BaseResultData.SUCCESS(usersService.list(queryWrapper));
    }


    //发布表信息表id集合查询
    @PostMapping("/listByLoginId")
    public List<UsersVo> listByLoginId(Integer id){
        return  usersService.listByLoginId(id);
    }


    //发布表信息系表id查询
    @PostMapping("/selUserById")
    public UsersVo selUserById(Integer id){
        return  usersService.selUsers(id);
    }

    //发布表信息添加
    @PostMapping("/insert")
    public BaseResultData insert(@RequestBody Users users){
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 8; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        users.setUid(sb.toString());
        //获取省
        String province = users.getProvince();
        //判断是否有省字
        if(isContain(province,"省")){
            String province1 = province.substring(0,province.length()-1);
            users.setProvince(province1);
        }
        //获取市
        String city = users.getCity();
        //判断是否有市字
        if(isContain(city,"市")) {
            String city1 = city.substring(0, city.length() - 1);
            users.setCity(city1);
        }
        return BaseResultData.SUCCESS(usersService.save(users));
    }

    //发布表信息接取是修改状态
    @PostMapping("/deleteId")
    public BaseResultData deleteId(Integer id){
        return BaseResultData.SUCCESS(usersService.delete(id))  ;
    }

    //发布表信息删除
    @PostMapping("/delete")
    public BaseResultData delete(Integer id){
        return BaseResultData.SUCCESS(usersService.removeById(id))  ;
    }

    //发布表信息重新发布
    @PostMapping("/updateId")
    public BaseResultData updateId(Integer id){
        return BaseResultData.SUCCESS(usersService.updateId(id))  ;
    }

    //发布表几个查询
    @PostMapping("/selUsersListByAllId")
    public List<UsersVo> selUsersListByAllId(int[] uid){
        List<UsersVo> list=new ArrayList<>();
        for (int i : uid) {
            UsersVo u=new UsersVo();
            Users byId = usersService.getById(i);
            u.setId(byId.getId());
            u.setUid(byId.getUid());
            u.setTypes(byId.getTypes());
            u.setNames(byId.getNames());
            u.setLoginId(byId.getLoginId());
            u.setSex(byId.getSex());
            u.setAge(byId.getAge());
            u.setPhone(byId.getPhone());
            u.setWx(byId.getWx());
            u.setProvince(byId.getProvince());
            u.setCity(byId.getCity());
            u.setCounty(byId.getCounty());
            u.setPlace(byId.getPlace());
            u.setClassCount(byId.getClassCount());
            u.setClassPlace(byId.getClassPlace());
            u.setSchool(byId.getSchool());
            u.setMajor(byId.getMajor());
            u.setIdentity(byId.getIdentity());
            u.setRegion(byId.getRegion());
            u.setMoney(byId.getMoney());
            u.setTimes(byId.getTimes());
            u.setSubsidy(byId.getSubsidy());
            u.setDescribes(byId.getDescribes());
            u.setRequirement(byId.getRequirement());
            u.setEnsure(byId.getEnsure());
            u.setCreateTime(byId.getCreateTime());
            u.setGrade(gradeService.getById(byId.getGradeId()));
            u.setSubject(subjectService.getById(byId.getSubjectId()));
            list.add(u);
        }
        return list;
    }

    //发布表修改
    @PostMapping("/update")
    public BaseResultData update(@RequestBody Users users){
        return BaseResultData.SUCCESS(usersService.saveOrUpdate(users))  ;
    }

    public static void main(String[] args) {
        String str="江苏";
        str=str.substring(0,str.indexOf("省"));
        System.out.println(str);
    }

    //判断是否包含某个字符
    public static boolean isContain(String s1,String s2) {
        return s1.contains(s2);
    }
}
